package webservice;

import java.sql.*;

public class Database {
	
	
	private Connection			conn	= null;
	private PreparedStatement	stmt	= null;
	
	
	public Database(String url, String user, String pass) throws SQLException, ClassNotFoundException {
		
		Class.forName(DBconfig.DRIVER);
		conn = DriverManager.getConnection(url, user, pass);
	}
	
	public void setCommand(String command) throws SQLException {
		
		if (stmt != null)
			stmt.close();
		
		stmt = conn.prepareStatement(command);
	}
	
	public ResultSet executeQuery() throws SQLException {
		
		return stmt.executeQuery();
		
	}
	
	public boolean execute() throws SQLException {
		
		return stmt.execute();
		
	}
	
	public void close() throws SQLException {
		
		if (conn != null)
			conn.close();

		if (stmt != null)
			stmt.close();
	}

	/*
	public static void main(String[] args)   {
		
		String query		= "SELECT * FROM users";

		Database db = null;
		
			try 
			{
				
				
				db = new Database(DBconfig.url, DBconfig.USER, DBconfig.PASS);
				
				db.setCommand(query);
				ResultSet rs = db.execute();
				ResultSetMetaData rsmd = rs.getMetaData();
				for ( int i = 1 ; i <= rsmd.getColumnCount() ; i++) {
					System.out.print(rsmd.getColumnName(i) + DBconfig.SEPAR);
				}
				
				while (rs.next()) {
					System.out.println("");
				    String name = rs.getString(1);
				    rsmd = rs.getMetaData();
					for ( int i = 1 ; i <= rsmd.getColumnCount() ; i++) {
						System.out.print(rs.getString(i) + DBconfig.SEPAR);
					}
					
				}
				

			} 
			catch (SQLException e) 
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
			catch (ClassNotFoundException e) 
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			finally
			{
				try {
					db.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
	}
	*/
	
}
